package com.ruoyi.yyq.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.security.annotation.InnerAuth;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.yyq.domain.CarRescueTask;
import com.ruoyi.yyq.service.ICarRescueTaskService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 救援订单Controller
 * 
 * @author ruoyi
 * @date 2025-05-10
 */
@RestController
@RequestMapping("/helptask")
@CrossOrigin
public class CarRescueTaskController extends BaseController
{
    @Autowired
    private ICarRescueTaskService carRescueTaskService;
    @Autowired
    private RabbitTemplate rabbitTemplate;

    //对外暴露接口 查询救援订单
    @InnerAuth
    @GetMapping("/getlist")
    public TableDataInfo getlist(CarRescueTask carRescueTask){

        List<CarRescueTask> list = carRescueTaskService.selectCarRescueTaskList(carRescueTask);
        System.out.println(list.toString());
        return getDataTable(list);
    }
    //对外暴露接口 添加救援订单
    @InnerAuth
    @PostMapping("/getadd")
    public AjaxResult getAdd(@RequestBody CarRescueTask carRescueTask){
        int result = carRescueTaskService.insertCarRescueTask(carRescueTask);
        if (result > 0) {
            // 发送订单信息到队列
            rabbitTemplate.convertAndSend("help_queue", carRescueTask);
            // 在控制台打印订单信息
            System.out.println("已发送救援信息到队列: " + carRescueTask);
        }
        return toAjax(result);
    }


    /**
     * 查询救援订单列表
     */
    @RequiresPermissions("help:helptask:list")
    @GetMapping("/list")
    public TableDataInfo list(CarRescueTask carRescueTask)
    {
        startPage();
        List<CarRescueTask> list = carRescueTaskService.selectCarRescueTaskList(carRescueTask);
        return getDataTable(list);
    }

    /**
     * 导出救援订单列表
     */
    @RequiresPermissions("help:helptask:export")
    @Log(title = "救援订单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CarRescueTask carRescueTask)
    {
        List<CarRescueTask> list = carRescueTaskService.selectCarRescueTaskList(carRescueTask);
        ExcelUtil<CarRescueTask> util = new ExcelUtil<CarRescueTask>(CarRescueTask.class);
        util.exportExcel(response, list, "救援订单数据");
    }

    /**
     * 获取救援订单详细信息
     */
    @RequiresPermissions("help:helptask:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(carRescueTaskService.selectCarRescueTaskById(id));
    }

    /**
     * 新增救援订单
     */
    @RequiresPermissions("help:helptask:add")
    @Log(title = "救援订单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CarRescueTask carRescueTask)
    {
        int result = carRescueTaskService.insertCarRescueTask(carRescueTask);
        if (result > 0) {
            // 发送订单信息到队列
            rabbitTemplate.convertAndSend("help_queue", carRescueTask);
            // 在控制台打印订单信息
            System.out.println("已发送救援信息到队列: " + carRescueTask);
        }
        return toAjax(result);
        //return toAjax(carRescueTaskService.insertCarRescueTask(carRescueTask));
    }

    /**
     * 修改救援订单
     */
    @RequiresPermissions("help:helptask:edit")
    @Log(title = "救援订单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CarRescueTask carRescueTask)
    {
        return toAjax(carRescueTaskService.updateCarRescueTask(carRescueTask));
    }

    /**
     * 删除救援订单
     */
    @RequiresPermissions("help:helptask:remove")
    @Log(title = "救援订单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(carRescueTaskService.deleteCarRescueTaskByIds(ids));
    }
}
